package com.byx.chatclient.im;

import com.byx.chatclient.entity.MessagePo;
import com.byx.chatclient.entity.message.AudioMsgBody;
import com.byx.chatclient.entity.message.FileMsgBody;
import com.byx.chatclient.entity.message.ImageMsgBody;
import com.byx.chatclient.entity.message.Message;
import com.byx.chatclient.entity.message.MsgBody;
import com.byx.chatclient.entity.message.MsgSendStatus;
import com.byx.chatclient.entity.message.MsgType;
import com.byx.chatclient.entity.message.TextMsgBody;
import com.byx.chatclient.entity.message.VideoMsgBody;
import com.byx.chatclient.utils.GsonUtil;
import com.byx.imclientutil.entity.BaseMessage;
import com.byx.imclientutil.entity.MessageContentType;
import com.byx.imclientutil.entity.MessageType;
import com.byx.imclientutil.utils.BaseMessageConverter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 * MessageConverter
 * 消息转换工具类
 * @author Superb
 * @date 2021/3/30 16:47
 */
public class MessageConverter extends BaseMessageConverter {
    /**
     * 将聊天消息转换成可发送的消息类型
     * @param message
     * @return
     */
    public static BaseMessage toImMessage(Message message, boolean isSingleChat){
        BaseMessage.Builder builder = new BaseMessage.Builder();
        MsgBody msgBody = message.getBody();
        if(msgBody instanceof TextMsgBody){
            builder.setContent(GsonUtil.getGson().toJson(msgBody));
            builder.setContentType(MessageContentType.TEXT);
        }else if (msgBody instanceof FileMsgBody){
            String json;
            if (msgBody instanceof ImageMsgBody){
                //拷贝本地路径
                String localPath = ((ImageMsgBody) msgBody).getThumbUrl();
                ((ImageMsgBody) msgBody).setThumbUrl("");
                json = new Gson().toJson(msgBody);
                System.out.println(json);
                //还原本地路径，以保存到本地数据库
                ((ImageMsgBody) msgBody).setThumbUrl(localPath);
                builder.setContentType(MessageContentType.IMAGE);
            }else if (msgBody instanceof AudioMsgBody){
                //拷贝本地路径
                String localPath = ((AudioMsgBody) msgBody).getLocalPath();
                ((AudioMsgBody) msgBody).setLocalPath("");
                json = new Gson().toJson(msgBody);
                System.out.println(json);
                //还原本地路径，以保存到本地数据库
                ((AudioMsgBody) msgBody).setLocalPath(localPath);
                builder.setContentType(MessageContentType.AUDIO);
            }else if (msgBody instanceof VideoMsgBody){
                //拷贝本地路径
                String localPath = ((VideoMsgBody) msgBody).getLocalPath();
                ((VideoMsgBody) msgBody).setLocalPath("");
                json = new Gson().toJson(msgBody);
                System.out.println(json);
                //还原本地路径，以保存到本地数据库
                ((VideoMsgBody) msgBody).setLocalPath(localPath);
                builder.setContentType(MessageContentType.VIDEO);
            }else {
                //拷贝本地路径
                String localPath = ((FileMsgBody) msgBody).getLocalPath();
                ((FileMsgBody) msgBody).setLocalPath("");
                json = new Gson().toJson(msgBody);
                System.out.println(json);
                //还原本地路径，以保存到本地数据库
                ((FileMsgBody) msgBody).setLocalPath(localPath);
                builder.setContentType(MessageContentType.FILE);
            }
            builder.setContent(json);
        }
        switch (message.getSentStatus()){
            case SENT:
                builder.setStatus(200);
                break;
            case FAILED:
            default:
                builder.setStatus(300);
                break;
        }
        int type = isSingleChat ? MessageType.SINGLE_CHAT_MSG : MessageType.GROUP_CHAT_MSG;
        return builder.setRecipientId(message.getTargetId())
                .setSenderId(message.getSenderId())
                .setType(type)
                .setTimestamp(message.getSentTime())
                .setId(message.getMsgId())
                .build();
    }

    /**
     * 将可发送的消息转换为界面上便于展示的聊天消息
     * @param baseMessage
     * @return
     */
    public static Message toMessage(BaseMessage baseMessage){
        Message message = new Message();
        MsgBody msgBody = null;
        switch (baseMessage.getContentType()){
            case MessageContentType.TEXT:
                msgBody = GsonUtil.getGson().fromJson(baseMessage.getContent(),TextMsgBody.class);
                message.setMsgType(MsgType.TEXT);
                break;
            case MessageContentType.FILE:
                msgBody = GsonUtil.getGson().fromJson(baseMessage.getContent(),FileMsgBody.class);
                message.setMsgType(MsgType.FILE);
                break;
            case MessageContentType.AUDIO:
                msgBody = GsonUtil.getGson().fromJson(baseMessage.getContent(),AudioMsgBody.class);
                message.setMsgType(MsgType.AUDIO);
                break;
            case MessageContentType.IMAGE:
                msgBody = GsonUtil.getGson().fromJson(baseMessage.getContent(),ImageMsgBody.class);
                message.setMsgType(MsgType.IMAGE);
                break;
            case MessageContentType.VIDEO:
                msgBody = GsonUtil.getGson().fromJson(baseMessage.getContent(),VideoMsgBody.class);
                message.setMsgType(MsgType.VIDEO);
                break;
            default:break;
        }
        message.setBody(msgBody);
        message.setMsgId(baseMessage.getId());
        message.setSenderId(baseMessage.getSenderId());
        message.setTargetId(baseMessage.getRecipientId());
        message.setSentTime(baseMessage.getTimestamp());
        message.setUuid(null);
        message.setSentStatus(MsgSendStatus.SENT);

        return message;
    }

    /**
     * 将App上可显示的消息转换为可存储的消息
     * @param message
     * @return
     */
    public static MessagePo toMessagePo(Message message, boolean isSingleChat){
        MessagePo.Builder builder = new MessagePo.Builder();
        MsgBody msgBody = message.getBody();
        if(msgBody instanceof TextMsgBody){
            builder.withContent(GsonUtil.getGson().toJson(msgBody,TextMsgBody.class));
            builder.withContentType(MessageContentType.TEXT);
        }else if (msgBody instanceof FileMsgBody){
            if (msgBody instanceof ImageMsgBody){
                builder.withContent(GsonUtil.getGson().toJson(msgBody,ImageMsgBody.class));
                builder.withContentType(MessageContentType.IMAGE);
            }else if (msgBody instanceof AudioMsgBody){
                builder.withContent(GsonUtil.getGson().toJson(msgBody,AudioMsgBody.class));
                builder.withContentType(MessageContentType.AUDIO);
            }else if (msgBody instanceof VideoMsgBody){
                builder.withContent(GsonUtil.getGson().toJson(msgBody,VideoMsgBody.class));
                builder.withContentType(MessageContentType.VIDEO);
            }else {
                builder.withContent(GsonUtil.getGson().toJson(msgBody,FileMsgBody.class));
                builder.withContentType(MessageContentType.FILE);
            }
        }
        switch (message.getSentStatus()){
            case SENT:
                builder.withStatus(200);
                break;
            case FAILED:
            default:
                builder.withStatus(300);
                break;
        }
        int type = isSingleChat ? MessageType.SINGLE_CHAT_MSG : MessageType.GROUP_CHAT_MSG;
        return builder.withRecipientId(message.getTargetId())
                .withSenderId(message.getSenderId())
                .withType(type)
                .withSendTimestamp(message.getSentTime())
                .withId(message.getMsgId())
                .build();
    }

    /**
     * 从本地数据库中取消息
     * @param messagePo
     * @return
     */
    public static Message toMessage(MessagePo messagePo){
        Message message = new Message();
        MsgBody msgBody = null;
        switch (messagePo.getContentType()){
            case MessageContentType.TEXT:
                msgBody = GsonUtil.getGson().fromJson(messagePo.getContent(),TextMsgBody.class);
                message.setMsgType(MsgType.TEXT);
                break;
            case MessageContentType.FILE:
                msgBody = GsonUtil.getGson().fromJson(messagePo.getContent(),FileMsgBody.class);
                message.setMsgType(MsgType.FILE);
                break;
            case MessageContentType.AUDIO:
                msgBody = GsonUtil.getGson().fromJson(messagePo.getContent(),AudioMsgBody.class);
                message.setMsgType(MsgType.AUDIO);
                break;
            case MessageContentType.IMAGE:
                msgBody = GsonUtil.getGson().fromJson(messagePo.getContent(),ImageMsgBody.class);
                message.setMsgType(MsgType.IMAGE);
                break;
            case MessageContentType.VIDEO:
                msgBody = GsonUtil.getGson().fromJson(messagePo.getContent(),VideoMsgBody.class);
                message.setMsgType(MsgType.VIDEO);
                break;
            default:break;
        }
        message.setBody(msgBody);
        message.setMsgId(messagePo.getId());
        message.setSenderId(messagePo.getSenderId());
        message.setTargetId(messagePo.getRecipientId());
        message.setSentTime(messagePo.getSendTimestamp());
        message.setUuid(null);
        message.setSentStatus(MsgSendStatus.SENT);

        return message;
    }
}
